class HCSaleLabel{constructor(e){this.domain=e.domain,this.selectors={product_page:".product__media-gallery, .product-media-main, .product-media-container, li, .product-detail__image, .swiper-slide",product_item:".card, .card__media, .grid-view-item-image, .indiv-product, .grid-view_image, .grid__item, .product__grid-item, .product, .product-card, .product-block__image, .swiper-slide, .product-item, .grid-product__content, .ProductItem, .grid-product__image-wrapper, .product-item__link, .product-grid-item__image,.grid-product__image-wrap, .product--root, .product-image__wrapper, .sf__image-box, .product-thumb-container,.reveal",recommendations:'#shopify-section-product-recommendations, .section-product-recommendations, .product-recommendations, .related-products, [data-url*="/recommendations/"]'},this.variants=e.variants||[],this.selectedVariant=e.selectedVariant||{},this.ppImages=e.productPageImages||[]}isProductPage(){return-1e.compare_at_price>e.price));if(null!=t)return 100*(t.compare_at_price-t.price)/t.compare_at_price}return 100*(e.compare_at_price_min-e.price_min)/e.compare_at_price_min}addLabel(e,t,i,a="hc-sale-tag"){let r=document.createElement("div");r.className=a,t<=0&&(r.style.display="none");let n=document.createElement("span");n.textContent=`${t.toFixed(0)}%`,r.appendChild(n);let o=e.closest(i);null==o&&(console.log("PARENT NODE SELECTED"),o=e.parentNode),o.prepend(r)}getProductPageElement(e,t,i=!1){let a=null;for(let r=0;r{let t=[`img[data-src*="${e=e.substring(0,e.lastIndexOf("."))}"]`,`img[srcset*="${e}"]:not(.product-sticky-form__image)`,`img[src*="${e}"]`,`img[data-original-src*="${e}"]`,`img[data-srcset*="${e}"]`,`a[href*="${e}"]`];const i=this.getProductPageElement(e,t);if(null!=i){let e=0;return this.selectedVariant.available&&this.selectedVariant.compare_at_price&&this.selectedVariant.compare_at_price>this.selectedVariant.price&&(e=100*(this.selectedVariant.compare_at_price-this.selectedVariant.price)/this.selectedVariant.compare_at_price),void this.addLabel(i,e,this.selectors.product_page,"hc-sale-tag hc-product-page")}}))}detectAjaxUrlChange(e,t,i,a=!1){function r(e,t){return function(){var i=e.apply(this,arguments);return t.apply(this,arguments),i}}function n(){const r=window.location.search.match(e);return r?r[1]!=i&&(i=r[1],"function"==typeof t)?t(i):void 0:a&&-1!==i.indexOf("filter")?t(""):void 0}window.history.pushState=r(history.pushState,n),window.history.replaceState=r(history.replaceState,n),window.addEventListener("popstate",n)}initVariantChangeEvent(){if(this.variants.length>1){let e=this.selectedVariant.id.toString();const t=e=>{let t=this.variants.find((t=>t.id==e));if(null!==t){var i=document.querySelectorAll(".hc-product-page.hc-sale-tag");if(t.compare_at_price&&t.compare_at_price>t.price){let e=100*(t.compare_at_price-t.price)/t.compare_at_price;if(e)for(var a=0;ae.price_min&&(null===t&&(console.log("ELEMENT IS NULL"),t=this.getProductItemElement(e)),void 0!==t)){const i=this.getDiscount(e)||0;this.addLabel(t,i,this.selectors.product_item)}}loadLabelsByHandle(e=document){const t=e.querySelectorAll('a[href*="/products/"]');t.forEach((e=>{let t,i;t=-1!==e.id.indexOf("CardLink")||e.classList.contains("increase-target")||e.classList.contains("stretched-link")||e.classList.contains("full-link")||e.classList.contains("hidden-product-link")||e.classList.contains("full-unstyled-link")||e.classList.contains("full-width-link")?e:e.querySelector("img")||e.querySelector(".product__media"),null!=t&&(i=e.href.match(".*\\/products\\/([^?]*)"),i&&(i=i[1],fetch(`https://${this.domain}/products/${i}.js`).then((e=>e.json())).then((e=>this.addProductItemLabel(e,t)))))}))}initPagesTracker(){this.detectAjaxUrlChange(".*page=(\\d+).*",(e=>{setTimeout(this.loadLabelsByHandle.bind(this),1200)}))}initAjaxFilter(){let e=window.location.search.replace("?","");this.detectAjaxUrlChange(".*?(?=filter|sort_by)(.*)",(e=>{setTimeout(this.loadLabelsByHandle.bind(this),1700)}),e,!0)}initProductPage(){this.addProductPageLabel(),this.initVariantChangeEvent()}initProductItems(){this.loadLabelsByHandle(),this.initPagesTracker(),this.initAjaxFilter(),document.addEventListener("globo.relatedproduct.loaded",(()=>setTimeout((()=>this.loadLabelsByHandle()),250))),window.addEventListener("globoFilterQuickviewRenderCompleted",(()=>setTimeout((()=>this.loadLabelsByHandle()),250))),window.addEventListener("globoFilterRenderSearchCompleted",(()=>setTimeout((()=>this.loadLabelsByHandle()),250))),window.addEventListener("globoFilterRenderCompleted",(()=>setTimeout((()=>this.loadLabelsByHandle()),250))),window.addEventListener("collectionUpdate",(()=>this.loadLabelsByHandle()))}init(){window.addEventListener("DOMContentLoaded",(e=>{this.isProductPage()&&this.initProductPage(),(this.isCollectionPage()||this.isSearchPage())&&this.initProductItems(),this.isHomePage()&&this.loadLabelsByHandle(),this.isCartPage()&&this.loadLabelsByHandle()})),window.addEventListener("load",(e=>{this.isProductPage()&&this.initProductRecommendations();const t=document.querySelector('[data-section-type*="recently-viewed"');null!==t&&this.loadLabelsByHandle(t);const i=document.querySelector("[id*=featured],[class*=featured-collection]");null!==i&&this.loadLabelsByHandle(i)}))}}const st=new HCSaleLabel(hcSaleLabelSettings);st.init();